define(function(require, exports, module) {

  // var Backbone = require('backbone');
  var $ = require('jquery');
  var _ = require('underscore');
  // var jsonview = require('jsonview');
  var tmpl = require('text!../tmpl.html');

  var CTable = require('hy5b/frontend/datatable/index')

  function main() {
    var $app = $('<div id="app">');
    $('#main').html($app);

    $app.append(tmpl);

    var json = {
      "errnum": 0,
      "errmsg": "",
      "result": [
        {"domain_id": 10001, "username": "admin", "password": "password", "identify": 0},
        {"domain_id": 10011, "username": "admin", "password": "qwert", "identify": 0},
        {"domain_id": 10002, "username": "dunchong", "password": "123456", "identify": 2},
        {"domain_id": 10003, "username": "yitian", "password": "ddmser133", "identify": 0},
        {"domain_id": 10004, "username": "hongxi", "password": "qwertyui", "identify": 1},
        {"domain_id": 10005, "username": "guandi", "password": "password", "identify": 0},],
      "other":0
    };

    var oT = new CTable();
    oT.setHead(["位置域", "用户名", "密码", "身份", "排序"])
      .setColumn(["domain_id", "username", "password",
        {
          key: "identify",
          format: function(value, index) {
            var role = ["超级管理员", "网络管理员", "日志管理员"];
            return (role[value]);
          }
        }, {
          key: 'username',
          mformat: function(value, i, atable) {
            var oret = { val: value }
            if (i > 0 && atable[i - 1].username === value) {
              oret.firstFlag = false;
            } else {
              oret.total = 1;
              for (var j = (i + 1); j < atable.length; j++) {
                if (atable[j].username === value) {
                  oret.total++
                } else {
                  break;
                }
              }
              oret.span = 'row'
              oret.firstFlag = true
            }
            return oret
          }
        }])
      .render(json.result, function(sbody, shtml) {
        $("#data").html(shtml);
      });
  }

  return main;
})
